angular.module('httpModule', [])
    .factory('httpFactory', ['$http', '$q', function ($http, $q) {
        var httpFactory = {};
        httpFactory.getData = function (url) {
            var res = $q.defer();

            httpRequestEvent('get', url).then(function (data) {
                res.resolve(data);
            });

            //httpRequest('get', url, function(data){
            //    res.resolve(data);
            //});

            return res.promise;
        };

        function httpRequestEvent(type, url) {
            var res = $q.defer();
            $http({
                method: type,
                url: url
            }).then(function successCallback(response) {
                res.resolve(response.data);
            }, function errorCallback(response) {
                res.reject("error");
            });
            return res.promise;
        }

        function httpRequest(type, url, callback){
            $http({
                method: type,
                url: url
            }).then(function successCallback(response) {
                callback(response.data);
            }, function errorCallback(response) {
                callback("error");
            });
        }

        return httpFactory; //这一行只能放最后
    }]);

